package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.model.Shop;
import com.x.erp.model.User;
import com.x.erp.rpc.ShopRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 车商Controller Created by gdf on 2018/1/26.
 */
@RestController
public class ShopController extends BaseController {

	@Reference
	private ShopRpcService shopRpcService;// 商家服务

	@Autowired
	private FastFileStorageClient fdfsClient;// 文件服务


	/**
	 * 查询车商列表
	 *
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "/getShopList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel getShopList(Shop shop) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", shopRpcService.selectShopList(shop));
	}


	/**
	 * 查询车商数量
	 *
	 * @param shop
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/selectShopNum", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel selectShopNum(Shop shop) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", shopRpcService.selectShopNum(shop));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}


	/**
	 * 插入更新店铺
	 *
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "/insertOrUpdateShop", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel insertOrUpdateShop(Shop shop, User user, @RequestParam(value = "shopLogoFile", required = false) MultipartFile shopLogoFile, @RequestParam(value = "shopLicenseFile", required = false) MultipartFile shopLicenseFile) {
		String fielId = null;
		if (shopLogoFile != null && !shopLogoFile.isEmpty()) {
			String delFileId = shop.getShopLogo();
			// 上传图片
			try {
				fielId = fdfsClient.uploadFile(shopLogoFile.getInputStream(), fdfsClient.getFileExt(shopLogoFile.getOriginalFilename()));
				if (fielId == null) {
					return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
				}
				shop.setShopLogo(fielId);
			} catch (Exception e) {
				e.printStackTrace();
				return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
			}
			// 删除原有图片
			if (StringUtils.isNotEmpty(delFileId)) {
				try {
					fdfsClient.deleteFile(delFileId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if (shopLicenseFile != null && !shopLicenseFile.isEmpty()) {
			String delFileId = shop.getShopBusinessLicense();
			// 上传图片
			try {
				fielId = fdfsClient.uploadFile(shopLicenseFile.getInputStream(), fdfsClient.getFileExt(shopLicenseFile.getOriginalFilename()));
				if (fielId == null) {
					return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
				}
				shop.setShopBusinessLicense(fielId);
			} catch (Exception e) {
				e.printStackTrace();
				return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
			}
			// 删除原有图片
			if (StringUtils.isNotEmpty(delFileId)) {
				try {
					fdfsClient.deleteFile(delFileId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		user.setShop(shop);
		if (this.isNotNull(shop.getShopId())) {
			shopRpcService.updateShopUser(user);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
		} else {
			shopRpcService.insertShopUser(user);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "新增成功！");
		}
	}



}
